//
//  ZDQTopicNewsTableViewCell.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZDQTopicNewsTableViewCell.h"


@implementation ZDQTopicNewsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self create];
    }
    
    return self;
    
}


- (void)setArray:(NSArray *)arr {
    
    if (_array != arr) {
        _array = arr;
    }
    
    [self.collectionView reloadData];
}



- (void)create {
    
    UICollectionViewFlowLayout * flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake(150 * SCREEN_WIDTH / 375, 120 * SCREEN_HEIGHT / 667);
    flowlayout.sectionInset = UIEdgeInsetsMake(10 * SCREEN_HEIGHT / 667, 20 * SCREEN_WIDTH / 375, 10 * SCREEN_HEIGHT / 667, 20 * SCREEN_WIDTH / 375);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:flowlayout];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[ZDQTpicNewsCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}



#pragma mark collectionView的dataSource和delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZDQTpicNewsCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    ZDQNewsTopic *news = self.array[indexPath.row];
    cell.titleLabel.text = news.title;
    [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:news.thumbnail] placeholderImage:[UIImage imageNamed:@"pac.png"]];
    cell.backgroundColor = [UIColor clearColor];
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.photoImageView.alpha = 0.4;
        self.collectionView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
    } else {
        
        cell.titleLabel.textColor = [UIColor blackColor];
        cell.photoImageView.alpha = 1;
        self.collectionView.backgroundColor = [UIColor whiteColor];
    }
    
    return cell;
    
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.and.left.and.bottom.and.right.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
    }];
    
    
}




#pragma mark collection的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.delegate didselect:indexPath];
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
